package cn.elead.it.sso.core.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.elead.it.sso.core.constant.GlobalVariable;
import cn.elead.it.sso.core.model.SsoUser;
import cn.elead.it.sso.core.store.SsoLoginStore;
import cn.elead.it.sso.core.store.SsoSessionIdHelper;
import cn.elead.it.sso.core.util.CookieUtil;
import cn.elead.it.sso.core.util.JWTUtils;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

/**
 * <p>
 * Web单点登录处理工具(cookie)
 * </p>
 *
 * @author luopeng
 */
public class SsoWebLoginHelper {

	/**
	 * 客户端登录
	 * 
	 * @param response
	 * @param sessionId
	 * @param ssoUser
	 * @param ifRemember true: cookie not expire, false: expire when browser close
	 * 
	 */
	public static void login(HttpServletResponse response, String sessionId, SsoUser ssoUser, boolean ifRemember) {
		// 当前session是否有效
		String storeKey = SsoSessionIdHelper.parseStoreKey(sessionId);
		if (storeKey == null) {
			throw new RuntimeException("parseStoreKey Fail, sessionId:" + sessionId);
		}
		// 将session存入redis
		SsoLoginStore.put(storeKey, ssoUser);
		// 将session添加到Cookie
		CookieUtil.set(response, GlobalVariable.SSO_SESSIONID, sessionId, ifRemember);
	}

	/**
	 * 客户端登出
	 * 
	 * @param request
	 * @param response
	 */
	public static void logout(HttpServletRequest request, HttpServletResponse response) {

		// 从Cookie中获取session
		String cookieSessionId =getSessionIdByCookie(request);
		if (cookieSessionId == null) {
			return;
		}
		// 解析session
		String storeKey = SsoSessionIdHelper.parseStoreKey(cookieSessionId);
		if (storeKey != null) {
			// 将session从redis中删除
			SsoLoginStore.remove(storeKey);
		}
		// 将session从Cookie中删除
		CookieUtil.remove(request, response, GlobalVariable.SSO_SESSIONID);
	}

	/**
	 * 登录校验
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public static SsoUser loginCheck(HttpServletRequest request, HttpServletResponse response) {

		// 从Cookie中获取session
		String cookieSessionId = getSessionIdByCookie(request);
		if (StrUtil.isBlank(cookieSessionId)) {
			// 不同域情况 第一次登录重定向过到客户端,客户端没有Cookie,则需要将地址中sessionId存入cookie
			// 同域情况 Cookie中的sessionId可能不是最新的,需要将地址中sessionId存入cookie
			// remove old cookie
			SsoWebLoginHelper.removeSessionIdByCookie(request, response);

			// set new cookie
			String paramSessionId = request.getParameter(GlobalVariable.SSO_SESSIONID);
			SsoUser ssoUser = SsoTokenLoginHelper.loginCheck(paramSessionId);
			if (ObjectUtil.isNotNull(ssoUser)) {
				CookieUtil.set(response, GlobalVariable.SSO_SESSIONID, paramSessionId, false); // expire when browser close （client cookie）
				return ssoUser;
			}
		}
		// redis获取用户
		SsoUser ssoUser = SsoTokenLoginHelper.loginCheck(cookieSessionId);
		// 获取到直接返回,否则失效
		if (ObjectUtil.isNotNull(ssoUser)) {
			return ssoUser;
		}

		return null;
	}

	/**
	 * 客户端退出,清除cookie
	 * 
	 * @param request
	 * @param response
	 */
	public static void removeSessionIdByCookie(HttpServletRequest request, HttpServletResponse response) {
		CookieUtil.remove(request, response, GlobalVariable.SSO_SESSIONID);
	}

	/**
	 * 从cookie中获取sessionId
	 * 
	 * @param request
	 * @param response
	 */
	public static String getSessionIdByCookie(HttpServletRequest request) {
		String cookieSessionId = CookieUtil.getValue(request, GlobalVariable.SSO_SESSIONID);
		if(StrUtil.isNotBlank(cookieSessionId)) {
			// 解码一次等到code
			String parseSession = JWTUtils.parseSession(cookieSessionId);
			if(StrUtil.isNotBlank(parseSession)) {
				// 拿到最终的session
				cookieSessionId = JWTUtils.parseSession(parseSession);
			}
		}
		return cookieSessionId;
	}

}
